/**
 * [Apmessages] author by $YourName$
 * @include "../../extclient/RowExpander.js"
 * @include "../../extclient/gridToExcel.js"
 * @include "../../extclient/SearchField.js"
 */
 
Ext.namespace('com.xlhd.admin');
Ext.namespace('com.xlhd.admin.apmessages');

/**
 * 查询表单
 * @class com.xlhd.admin.apmessages.queryformpanel
 * @extends Ext.form.FormPanel
 */
com.xlhd.admin.apmessages.queryformpanel = Ext.extend(Ext.form.FormPanel,{
        initComponent:function() {
                Ext.apply(this,{
                labelAlign:'right',
                labelWidth:80,
                defaultType:'textfield',
                bodyStyle:'padding:20px;',
                defaults:{width:290},
                items:[{
                    xtype:'panel',
                    html:'请在下面输入查询条件：',
                    width:370,
                    border:false,
                    style:'padding:10 0 0 3;margin:0 0 20 10;border-bottom:1px solid #ccc;font-size:14px;font-weight:bold;'
                        }
                        ,{
                    xtype:'panel',
                    layout:'column',
                    width:400,
                    border:false,
                    defaults:{border:false}
                        }

                        ,{xtype:'textfield',fieldLabel:'msg',name:'s_msg',width:288}
                        ,{xtype:'textfield',fieldLabel:'filename',name:'s_filename',width:288}
                        ,{xtype:'textfield',fieldLabel:'fileurl',name:'s_fileurl',width:288}
                        ,{xtype:'textfield',fieldLabel:'infotype',name:'s_infotype',width:288}
                        ,{xtype:'textfield',fieldLabel:'canReply',name:'s_canReply',width:288}
                        ,{xtype:'textfield',fieldLabel:'sendTime',name:'s_sendTime',width:288}
                        ,{xtype:'textfield',fieldLabel:'sysflag',name:'s_sysflag',width:288}
                        ,{xtype:'textfield',fieldLabel:'dialogid',name:'s_dialogid',width:288}
                        ,{xtype:'textfield',fieldLabel:'ischecked',name:'s_ischecked',width:288}
                        ,{xtype:'textfield',fieldLabel:'isread',name:'s_isread',width:288}
                        ,{xtype:'textfield',fieldLabel:'ispush',name:'s_ispush',width:288}
                    ]
            });
                com.xlhd.admin.apmessages.queryformpanel.superclass.initComponent.call(this);
        }
});

/**
 * 查询窗口
 * @class com.xlhd.admin.apmessages.querywin
 * @extends Ext.Window
 */
com.xlhd.admin.apmessages.querywin = Ext.extend(Ext.Window,{
        initComponent:function() {
                Ext.apply(this,{
                title:'高级查询',
                width:455,
                height:395,
                modal:true,
                closeAction:'hide',
                layout:'fit'
            });
                com.xlhd.admin.apmessages.querywin.superclass.initComponent.call(this);
        }
});

/**
 * 内容表单
 * @class com.xlhd.admin.apmessages.dtlformpanel
 * @extends Ext.form.FormPanel
 */
com.xlhd.admin.apmessages.dtlformpanel = Ext.extend(Ext.form.FormPanel,{
        initComponent:function() {
                Ext.apply(this,{
                labelWidth:100,
                labelAlign:'right',
                frame:true,
//              bodyStyle:'padding:10px',
                autoScroll:true,//滚动条
                        items:[{
                            xtype:'panel',
                            layout:'column',
                            width:400,
                            border:false,
                            defaults:{border:false}
                                }
                                ,{xtype:'hidden',fieldLabel:'id',name:'id',width:288}
                                ,{xtype:'textfield',fieldLabel:'msg',name:'msg',width:288}
                                ,{xtype:'textfield',fieldLabel:'filename',name:'filename',width:288}
                                ,{xtype:'textfield',fieldLabel:'fileurl',name:'fileurl',width:288}
                                ,{xtype:'textfield',fieldLabel:'infotype',name:'infotype',width:288}
                                ,{xtype:'textfield',fieldLabel:'canReply',name:'canReply',width:288}
                                ,{xtype:'textfield',fieldLabel:'sendTime',name:'sendTime',width:288}
                                ,{xtype:'textfield',fieldLabel:'sysflag',name:'sysflag',width:288}
                                ,{xtype:'textfield',fieldLabel:'dialogid',name:'dialogid',width:288}
                                ,{xtype:'textfield',fieldLabel:'ischecked',name:'ischecked',width:288}
                                ,{xtype:'textfield',fieldLabel:'isread',name:'isread',width:288}
                                ,{xtype:'textfield',fieldLabel:'ispush',name:'ispush',width:288}
                ]
            });
            com.xlhd.admin.apmessages.dtlformpanel .superclass.initComponent.call(this);
        }
       
});

/**
 * 表单窗口
 * @class com.xlhd.admin.apmessages.dtlwin
 * @extends Ext.Window
 */            
com.xlhd.admin.apmessages.dtlwin =  Ext.extend(Ext.Window,{
        initComponent:function() {
                Ext.apply(this,{
                width:535,
                height:400,
                layout:'fit',
                border:false,
                closeAction:'hide',
                modal:true,
                maximizable:true,
                constrain: true,
                collapsible:true
            });
                com.xlhd.admin.apmessages.dtlwin.superclass.initComponent.call(this);
        }
});


/**
 * 主表格入口
 * @class com.xlhd.admin.apmessages
 * @extends Ext.grid.GridPanel
 */
com.xlhd.admin.apmessagesGrid = Ext.extend(Ext.grid.GridPanel,{
    initComponent:function() {
        this.pageSize=10;
        this.ds = new Ext.data.Store({
                url:'/Apmessages/extlist.do',
                reader:new Ext.data.JsonReader({
                    root:'list',
                    totalProperty:'totalSize',
                    id:'id'
                        }
                        ,['id','msg','filename','fileurl','infotype','canReply','sendTime','sysflag','dialogid','ischecked','isread','ispush',]
                ),
                baseParams:{
                    limit:this.pageSize
                },
                remoteSort:true
            });
           
            //行扩展
            this.expander = new Ext.grid.RowExpander({
                tpl : new Ext.Template(
                    '<p style="margin-left:70px"><b>字典内容:</b> {kvalue}</p><br>'
                )
            });
           
            this.sm = new Ext.grid.CheckboxSelectionModel();
                this.cm = new Ext.grid.ColumnModel([
                    new Ext.grid.RowNumberer(),
                    this.sm,
                    this.expander

                ,{header:'msg',width:100,sortable:true,dataIndex:'msg'}
                ,{header:'filename',width:100,sortable:true,dataIndex:'filename'}
                ,{header:'fileurl',width:100,sortable:true,dataIndex:'fileurl'}
                ,{header:'infotype',width:100,sortable:true,dataIndex:'infotype'}
                ,{header:'canReply',width:100,sortable:true,dataIndex:'canReply'}
                ,{header:'sendTime',width:100,sortable:true,dataIndex:'sendTime'}
                ,{header:'sysflag',width:100,sortable:true,dataIndex:'sysflag'}
                ,{header:'dialogid',width:100,sortable:true,dataIndex:'dialogid'}
                ,{header:'ischecked',width:100,sortable:true,dataIndex:'ischecked'}
                ,{header:'isread',width:100,sortable:true,dataIndex:'isread'}
                ,{header:'ispush',width:100,sortable:true,dataIndex:'ispush'}
                ]);
               
                /**
                 * 扩展类的构建开始
                 */
                Ext.apply(this,{
                        store:this.ds
                ,sm:this.sm
                ,cm: this.cm
                        ,plugins:this.expander
                        ,collapsible: true
                        ,viewConfig:{forceFit:true}
                ,bbar:new Ext.PagingToolbar({
                    pageSize:this.pageSize,
                    store:this.ds,
                    displayInfo:true
                })
                , tbar:[
                        {text:'新增',cls:'x-btn-text-icon',iconCls:'addicon',handler:this.addApmessages,scope:this},'-'
                        ,{text:'修改',cls:'x-btn-text-icon',iconCls:'editicon',handler:this.editApmessages,scope:this},'-'
                        ,{text:'删除',cls:'x-btn-text-icon',iconCls:'deleteicon',handler:this.deleteApmessages,scope:this},'-'
                        ,{text:'查询',id:'btn-query',cls:'x-btn-text-icon',iconCls:'queryicon',handler:this.buildQueryWin,scope:this}
                                ,{text:'导出到Excel', cls:'x-btn-text-icon',iconCls:'exporticon',handler:this.exporttoexcel,scope:this}
                        ,'->'
                        ,'搜索范围：'
                                ,{xtype:'combo',
                    fieldLabel:'搜索范围',
                    emptyText:'请选择...',
                    name:'field_type',
                    hiddenName:'field_type',
//                    store:new Ext.data.Store({
//                        proxy:new Ext.data.HttpProxy({url:'/Apmessages/extfield.do'}),
//                        reader:new Ext.data.JsonReader({}, ['code','name']),
//                        autoLoad:false
//                    }),
                    store:new Ext.data.ArrayStore({
            			fields: ['name','code'],
            			data: [['msg', 'msg'],['filename', 'filename'],['fileurl', 'fileurl'],['infotype', 'infotype'],['canReply', 'canReply'],['sendTime', 'sendTime'],['sysflag', 'sysflag'],['dialogid', 'dialogid'],['ischecked', 'ischecked'],['isread', 'isread'],['ispush', 'ispush']]
            		}),
                    displayField:'name',
                    valueField:'code',
                    forceSelection: false,
                    selectOnFocus: true,
                    editable: false,
                    triggerAction: 'all',
                    allowBlank:true,
                    mode: 'local',
                    width:120
                    ,listeners: {          
                                select:{fn:function(object,record,index){
                                        this.getTopToolbar().items.get("searchfld").getStore().baseParams['field_type'] = object.getValue();
                                },scope:this}
                        }    
                        },{xtype:"searchfield",itemId:"searchfld",width: 130,store:this.ds}
                ]
                });
                //调用父类构建函数
        com.xlhd.admin.apmessagesGrid.superclass.initComponent.call(this);
        //加载数据
        this.store.load({params:{start:0}});
       
                //扩展类的详细弹出窗口
                this.dtlformpanel = new com.xlhd.admin.apmessages.dtlformpanel();
                this.dtlwin =  new com.xlhd.admin.apmessages.dtlwin({items:this.dtlformpanel,buttons:[{
                    text:'保存',
                    handler:this.saveApmessages,
                    scope:this
                },{
                    text:'取消',
                    handler:function(){this.dtlwin.hide();},
                    scope:this
                }]});
           
            //扩展类的查询弹出窗口
            this.queryformpanel = new com.xlhd.admin.apmessages.queryformpanel();
            this.querywin =  new com.xlhd.admin.apmessages.querywin({items:this.queryformpanel,buttons:[{
                    text:'确定',
                    handler:this.queryOrder,
                    scope:this
                },{
                    text:'取消',
                    handler:function(){this.querywin.hide();},
                    scope:this
                }]});
            //双击操作
                this.on({"dblclick":this.dblclick});
                //右键菜单监听
                this.addListener('rowcontextmenu', this.onMessageContextMenu);
    }
   
   /**
    * 构建函数结束
    */

    //导出excel方法
        ,exporttoexcel:function(){
        var vExportContent = this.getExcelXml();
        if (Ext.isIE6 || Ext.isIE7 || Ext.isSafari || Ext.isSafari2 || Ext.isSafari3) {
            var fd=Ext.get('frmDummy');
            if (!fd) {
                fd=Ext.DomHelper.append(Ext.getBody(),{tag:'form',method:'post',id:'frmDummy',action:'../../exportexcel.jsp', target:'_blank',name:'frmDummy',cls:'x-hidden',cn:[
                    {tag:'input',name:'exportContent',id:'exportContent',type:'hidden'}
                ]},true);
            }
            fd.child('#exportContent').set({value:vExportContent});
            fd.dom.submit();
        } else {
            document.location = 'data:application/vnd.ms-excel;base64,'+Base64.encode(vExportContent);
        }
        }
        //右键菜单
    ,onMessageContextMenu : function (grid, rowIndex, e) {
                e.stopEvent();
                var coords = e.getXY();
                var record = grid.getStore().getAt(rowIndex);
                var messageContextMenu = new Ext.menu.Menu({
                        id: 'messageContextMenu',
                        items: [{icon:'../../images/edit.gif',text: '编辑',handler: rgtEdit,scope: this},
                                {id: 'delete',icon:'../../images/delete.gif',handler: rgtDelete,text: '删除'
                }]
            });
            //右键编辑
            function rgtEdit() {
                                messageContextMenu.hide();
                                        this.dtlwin.setTitle('修改Apmessages');
                                        this.dtlwin.show();
                                        this.dtlformpanel.form.reset();
                                        this.dtlformpanel.form.loadRecord(record);
                                        this.dtlformpanel.url = '/Apmessages/extupdate.do';
            };
            //右键删除
                function rgtDelete() {
                        messageContextMenu.hide();
                        if (!record||record.length == 0) {
                                Ext.Msg.alert("提示", "请先选择要删除的�记录");
                                return;
                        }
                        Ext.MessageBox.confirm('确认删除','确定要删除这些记录?',function(btn){
                                if (btn == 'yes'){
                                                Ext.Ajax.request({
                                                url:'/Apmessages/extdelete.do?ids='+record.data.id,
                                                method:'POST',
                                                success:function(response){
                                                        var data = Ext.util.JSON.decode(response.responseText);
                                                        if (data.success == true){
                                                                grid.getStore().remove(record);
                                                                grid.getView().refresh();
                                                        }
                                                        else{
                                                                Ext.MessageBox.alert('警告',data.msg);
                                                        }
                                                         grid.getStore().reload();
                                                },
                                                scope:this
                                        });
                                }},this);
                };
                messageContextMenu.showAt([coords[0], coords[1]]);
                e.preventDefault();//to disable the standard browser context menu
        }
       
        //双击事件
    ,dblclick :function(){
                var sm = this.getSelectionModel();
                        var record=null;
                        try{
                                record=sm.getSelected();
                                if(record==null){
                                        return;
                                }
                        }
                        catch(e){
                                try{
                                        record=sm.selection.record();
                                }
                                catch(ex){}
                        }
                this.showWinForm(record);
        }
        //双击打开窗口
    ,showWinForm:function(record){
        this.dtlwin.setTitle('修改Apmessages');
        this.dtlwin.show();
        this.dtlformpanel.form.reset();
        this.dtlformpanel.form.loadRecord(record);
        this.dtlformpanel.url = '/Apmessages/extupdate.do';
    }
   
    //新建窗口
    ,addApmessages : function(){
        this.dtlwin.setTitle('新建Apmessages');
        this.dtlwin.show();
        this.dtlformpanel.form.reset();
            this.dtlformpanel.url = '/Apmessages/extsave.do';
        }
       
        //编辑操作
    ,editApmessages:function(){
        var records = this.getSelectionModel().getSelections();//单选
       
           if (records.length!=1) {
                        Ext.Msg.alert("提示", "请先选择要修改的记录");
                        return;
                }
            this.dtlwin.setTitle('修改Apmessages');
            this.dtlwin.show();
            this.dtlformpanel.form.reset();
            this.dtlformpanel.form.loadRecord(records[0]);
            this.dtlformpanel.url = '/Apmessages/extupdate.do';

    }
   
    //删除操作
    ,deleteApmessages:function(){
        var records = this.getSelectionModel().getSelections();
                if (!records||records.length == 0) {
                        Ext.Msg.alert("提示", "请先选择要删除的�记录");
                        return;
                }
                Ext.MessageBox.confirm('确认删除','确定要删除这些记录?',function(btn){
                        if (btn == 'yes'){
                                Ext.Ajax.request({
                                        url:'/Apmessages/extdelete.do?ids='+this.getRecordArrayUtils(records, 'id'),
                            method:'POST',
                            success:function(response){
                                var data = Ext.util.JSON.decode(response.responseText);
                                if (data.success == true){
                                        for(var i = 0; i < records.length; i++) {
                                                                this.getStore().remove(records[i]);
                                            this.getView().refresh();
                                                         }
                                                         this.getStore().reload();
                                }
                                else{
                                    Ext.MessageBox.alert('警告',data.msg);
                                }
                            },
                            scope:this
                        });
                        }
                },this);
    }
   
    //保存操作
    ,saveApmessages:function(){
                if (this.dtlformpanel.form.isValid() == false){
                return;
            }
            this.dtlformpanel.form.submit({
                url:this.dtlformpanel.url,
                success:function(form,action){
                        Ext.MessageBox.alert('警告',action.result.msg);
                    this.dtlwin.hide();
                        this.getStore().reload();
                },
                scope:this,
                failure:function(form,action){
                    Ext.MessageBox.alert('警告',action.result.msg);
                }
            })
       
    }
    //新建查询窗口
    ,buildQueryWin: function(){
        this.querywin.setTitle('查询');
        this.querywin.show();
        this.getTopToolbar().items.get("searchfld").setValue("");
        this.getTopToolbar().items.get("searchfld").getStore().baseParams['field_type']=null;
    }
    //查询操作
    ,queryOrder:function(){
        this.getStore().baseParams['s_msg'] = this.queryformpanel.form.findField('s_msg').getRawValue();
        this.getStore().baseParams['s_filename'] = this.queryformpanel.form.findField('s_filename').getRawValue();
        this.getStore().baseParams['s_fileurl'] = this.queryformpanel.form.findField('s_fileurl').getRawValue();
        this.getStore().baseParams['s_infotype'] = this.queryformpanel.form.findField('s_infotype').getRawValue();
        this.getStore().baseParams['s_canReply'] = this.queryformpanel.form.findField('s_canReply').getRawValue();
        this.getStore().baseParams['s_sendTime'] = this.queryformpanel.form.findField('s_sendTime').getRawValue();
        this.getStore().baseParams['s_sysflag'] = this.queryformpanel.form.findField('s_sysflag').getRawValue();
        this.getStore().baseParams['s_dialogid'] = this.queryformpanel.form.findField('s_dialogid').getRawValue();
        this.getStore().baseParams['s_ischecked'] = this.queryformpanel.form.findField('s_ischecked').getRawValue();
        this.getStore().baseParams['s_isread'] = this.queryformpanel.form.findField('s_isread').getRawValue();
        this.getStore().baseParams['s_ispush'] = this.queryformpanel.form.findField('s_ispush').getRawValue();
                this.getStore().load();
                this.querywin.hide();
    }
    //工具类
    ,getRecordArrayUtils : function(records,field) {
                var result = [];
                for(var i = 0; i < records.length; i++) {
                        result.push(records[i].get(field));
                }
                return result;
        }

});
 
/**
 * 注册主表格的xtype
 */
Ext.reg('apmessages', com.xlhd.admin.apmessagesGrid);
